package ch.elca.lol.puissance4;

import ch.elca.lol.puissance4.board.Board;
import ch.elca.lol.puissance4.board.FastImBoard;
import ch.elca.lol.puissance4.game.Game;
import ch.elca.lol.puissance4.game.PlayerLoader;
import ch.elca.lol.puissance4.game.Token;
import ch.elca.lol.puissance4.player.Player;

public class Puissance4Test {
	private static void wait(int millis) {
		try {
			Thread.sleep(millis);
		} catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	static int N = 4;
	static int n = 5;
	
	public static void main(String... args) {
		System.out.print("Waiting for a bit...");
		wait(5000);
		System.out.println(" done waiting.");
		
		// First do some iterations to fire up the JIT Compiler
		System.out.println("Warming up...");
		for(int i = 0; i < N / 4; i++) {
			test(new FastImBoard(7, 6));
		}
		
		System.out.println(N + " times " + n + " iterations.");
		for(int i = 0; i < N; i++) {
			test(new FastImBoard(7, 6));
		}
	}
	
	private static void test(Board b) {		
		Timer t = new Timer(b.getClass().getSimpleName());
		t.start();
		for(int i = 0; i < n; i++) {
			Player p = PlayerLoader.loadPlayer("ch.elca.lol.cake.CakeAI");
			p.setToken(Token.X);
			
			Player q = PlayerLoader.loadPlayer("ch.elca.lol.cake.CakeAI");
			q.setToken(Token.O);
			
			Game g = new Game(p, q, b);
			g.configure(false, 0);
			g.play();
		}
		t.stop();
		t.print();
	}
	
	static class Timer {
		private long sum = 0;
		private long last = 0;
		private final String name;
		
		public Timer(String name) {
			this.name = name;
		}
		
		public static long time() {
			return System.currentTimeMillis();
		}
		
		public void start() {
			last = time();
		}
		
		public void stop() {
			sum += (time() - last);
		}
		
		public void print() {
			System.out.println(name + "\t " + sum + " ms");
		}
	}
}
